<?php

declare(strict_types=1);

namespace app\agent\controller;

use app\model\AgentServiceConfig;
use app\model\ChannelServDiscount;
use app\model\ServiceSub;
use app\model\Template as Model;

class Template extends Controller
{
    /**
     * @api {GET} /agent/template 模板列表
     * @apiDescription 模板列表【目前用在渠道管理功能中对应业务配置】
     * @apiName agentTemplateIndex 模板列表
     * @apiGroup Admin2_0_agent
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function index()
    {
        // 获取 折扣配置最低值, 其中话费从agent表获取数据 其余的从昂 agent_service_config获取
        $agentService = AgentServiceConfig::where('status', 1)
            ->where('agent_id', $this->agent['id'])
            ->column('*', 'serv_type');
        //dd($agentService);
        //代理拥有的服务类型
        $serv_types = array_keys($agentService);

        $data = Model::with(['service.sub' => function($query) use ($serv_types){
            $query->whereNotIn('serv_type',[PRODUCT_MEAL_SERV])->whereIn('serv_type', $serv_types);//这个类型的删了，所以不显示了
        }])
            ->where('status', 1)
//            ->whereNotIn('serv_type',[PRODUCT_MEAL_SERV])
            ->order('sort desc, id desc')
            ->select()
            ->toArray();
        //dd($data);
        if (empty($data)) {
            return success('', []);
        }
        if ($agentService) {
            foreach ($data as $k1 => $v1) { // template 循环
                //查看该木板下是否拥有代理的服务类型，要是都没有的话，就不展示该模板
                if (empty($v1['service'])) {
                  unset($data[$k1]);
                  continue;
                }
                foreach ($v1['service'] as $k2 => $v2) { // template->service

                    foreach ($v2['sub'] as $k3 => $v3) { // service 下的子业务类型
                        $agentLowerPrice = $agentService[$v3['serv_type']]['agent_lower_price'] ?? 0;
                        // 返佣的，不能低于代理商成本价
                        if ($this->agent['settle_type'] == SETTLE_TYPE_PROFIT) {
                            $agentLowerPrice = 0;
                        }

                        // step 话费
                        if (PRODUCT_MOBILE_SERV == $v3['serv_type']) {
                            $data[$k1]['service'][$k2]['sub'] = [
                                [
                                    'id' => 1,
                                    'serv_type' => PRODUCT_MEAL_SERV,
                                    'sub_type' => SLOW_RECHARGE,
                                    'name' => '慢充',
                                    'permit_status' => isset($agentService[$v3['serv_type']]) && !empty($agentService[$v3['serv_type']])?ChannelServDiscount::STATE_AVTIVE:ChannelServDiscount::STATE_STOP,
                                    'value_min' => $this->agent['mobile_discount_slow'],
                                    'agent_lower_price' => $agentLowerPrice,
                                ],
                                [
                                    'id' => 2,
                                    'serv_type' => PRODUCT_MOBILE_SERV,
                                    'sub_type' => FAST_RECHARGE,
                                    'name' => '快充',
                                    'permit_status' => isset($agentService[$v3['serv_type']]) && !empty($agentService[$v3['serv_type']])?ChannelServDiscount::STATE_AVTIVE:ChannelServDiscount::STATE_STOP,
                                    'value_min' => $this->agent['mobile_discount_fast'],
                                    'agent_lower_price' => $agentLowerPrice,
                                ]
                            ];
                            // if (SLOW_RECHARGE == $v3['sub_type']) {
                            //     $data[$k1]['service'][$k2]['sub'][$k3]['value_min'] = $this->agent['mobile_discount_slow'];
                            // } else {
                            //     $data[$k1]['service'][$k2]['sub'][$k3]['value_min'] = $this->agent['mobile_discount_fast'];
                            // }
                        } else {
                            if (FAST_RECHARGE == $v3['sub_type']) {
                                $data[$k1]['service'][$k2]['sub'][$k3]['value_min'] = $agentService[$v3['serv_type']]['discount_fast'] ?? '';
                            } elseif (SLOW_RECHARGE == $v3['sub_type']) {
                                $data[$k1]['service'][$k2]['sub'][$k3]['value_min'] = $agentService[$v3['serv_type']]['discount_slow'] ?? '';
                            } elseif (3 == $v3['sub_type']) {
                                $data[$k1]['service'][$k2]['sub'][$k3]['value_min'] = ''; // 不需要设置
                            } else {
                                $data[$k1]['service'][$k2]['sub'][$k3]['value_min'] = $agentService[$v3['serv_type']]['value'] ?? '';
                            }
                            $data[$k1]['service'][$k2]['sub'][$k3]['agent_lower_price'] = $agentLowerPrice;
                            $data[$k1]['service'][$k2]['sub'][$k3]['permit_status'] = isset($agentService[$v3['serv_type']]) && !empty($agentService[$v3['serv_type']])?ChannelServDiscount::STATE_AVTIVE:ChannelServDiscount::STATE_STOP;

                        }
                    }
                }
            }
        }
        return success('', array_values($data));
    }

    // /*
    //  初始化 代理商业务字类数据
    // */
    // public function initServiceSubCat()
    // {
    //     $res = (new ServiceSub())->batchAdd();
    //     halt($res);
    // }
}
